package com.example.demo;

import org.junit.jupiter.api.Test;

public class String2Integer {

    @Test
    public void test() {
        int num = string2Integer("-3121");
        System.out.println(num);
    }

    private int string2Integer(String str) {
        int sign = 1;
        int res = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                continue;
            }
            if (str.charAt(i) == '-') {
                sign = -1;
                continue;
            }
            if (str.charAt(i) == '+') {
                continue;
            }
            res = res * 10 + str.charAt(i) - 48;
        }
        return res * sign;
    }
}
